/**
 * 抢单池
 */
package com.ruoyi.project.pay;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.Md5Utils;
import com.ruoyi.project.channel.businessInfoOrder.service.IBusinessInfoOrderService;
import com.ruoyi.project.channel.channelOrder.domain.ChannelOrder;
import com.ruoyi.project.channel.channelOrder.service.IChannelOrderService;
import com.ruoyi.project.channel.payerBusinessInfo.domain.PayerBusinessInfo;
import com.ruoyi.project.channel.payerBusinessInfo.service.IPayerBusinessInfoService;
import com.ruoyi.project.channel.payerInfo.domain.PayerInfo;
import com.ruoyi.project.channel.vwBusinessCashDayStat.domain.VwBusinessCashDayStat;
import com.ruoyi.project.channel.vwBusinessCashDayStat.service.IVwBusinessCashDayStatService;
import com.ruoyi.project.system.config.service.IConfigService;
import com.ruoyi.project.system.user.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class Grabbing {
    private static final Logger log = LoggerFactory.getLogger(Grabbing.class);
    @Autowired
    private IConfigService configService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private IVwBusinessCashDayStatService vwBusinessCashDayStatService;
    @Autowired
    private IPayerBusinessInfoService payerBusinessInfoService;
//    @Autowired
//    private IBusinessInfoOrderService businessInfoOrderService;
//    public static ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();ConcurrentHashMap

//    @Scheduled(cron = "0/60 * * * * ?")
//    private void configureTasks() {
//        log.info("Grabbing: run");
//        String grabbingOrders_ = configService.selectConfigByKey("grabbing_orders");
//        String[] grabbings = grabbingOrders_.split(";");
//        Date date = new Date();
//        int curHour= date.getHours();
//        for(int i=0;i<grabbings.length;i++){
//            String[] times = grabbings[i].split(",");
//            int hour = Integer.parseInt(times[0].trim());
////            // 墨西哥时间比香港时间晚13个小时， 也可以当做早11个小时计算
////            int hour = _hour + 11;
////            if(hour >=24){
////                hour = hour - 24;
////            }
//
//            int sum = Integer.parseInt(times[1].trim());
//            if(curHour != hour || !concurrentHashMap.contains(hour+"")) {
//                // 当前未到抢单时间抢单池数量初始化
//                concurrentHashMap.put(hour+"", sum+"");
//            }
//        }
//        log.info("concurrentHashMap:" + concurrentHashMap.toString());
//    }

    @Scheduled(cron = "0 0 0/4 * * ?")
    private void syncSalesmanDataToCenter() {
        if(!payConfig.isNeedSyncToPayCenter()) {
            return;
        }
        // 每天晚上0点执行一次
        log.info("syncSalesmanDataToCenter: run");
        List<VwBusinessCashDayStat> vwBusinessCashDayStats = vwBusinessCashDayStatService.selectVwSalesmanBusinessCashSyncList(new VwBusinessCashDayStat());
        PayerBusinessInfo search = new PayerBusinessInfo();
        search.setBusinessName("代理中心商户");
        List<PayerBusinessInfo> payerBusinessInfos = payerBusinessInfoService.selectPayerBusinessInfoList(search);
        PayerBusinessInfo payerBusinessInfo = payerBusinessInfos.get(0);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("businessId=").append(payerBusinessInfo.getBusinessNo());
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        stringBuilder.append("&time=").append(time);
        stringBuilder.append("&xksiekerhdk12shskdfje03943kdjdkJHJGJKHJHJHG");
        String sign = "";
        try {
            log.info("setToNextOrderString:" + stringBuilder.toString());
            sign = Md5Utils.androidMD5(stringBuilder.toString());
            System.out.println("setToNextOrderStringSign:" + sign);
        } catch (Exception e) {
        }
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("businessId", payerBusinessInfo.getBusinessNo());
        jsonObject.put("time", time);
        jsonObject.put("sign", sign);
        jsonObject.put("businessSalesmanStats", vwBusinessCashDayStats); // usdt地址
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
        String url = payConfig.getCaiNiaoHost() + "/pay/updateScoresStat";
        JSONObject body = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
        log.info("/pay/updateScoresStat:" + body.toJSONString());
    }
}
//public class Grabbing {
//    private static final Logger log = LoggerFactory.getLogger(Grabbing.class);
//    private IConfigService configService;
//    public static ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
//    public Grabbing(){
//        configService = SpringContextUtil.getBean(IConfigService.class);
//        log.info("Grabbing: start");
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                try {
//
//                } catch (Exception e) {
//                    log.info("concurrentHashMap error:" + concurrentHashMap.toString());
//                    log.error(e.getMessage(),e);
//                }
//            }
//        },0,60 * 1000); // 每30分钟检查一次
//    }
//}
